//
//  LSZHistoricalRecordOpinionsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/15.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZHistoricalRecordOpinionsVC.h"
#import "LSZResponseServiceTableViewCell.h"

@interface LSZHistoricalRecordOpinionsVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * arrayFeedback;
@property(nonatomic,strong) UITableView * tableViewOpinion;
@end

@implementation LSZHistoricalRecordOpinionsVC
-(void)getData :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic feedbackList:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOk, NSString * string, LSZFeedbackListManager * manager) {
        if (isOk == YES) {
            if (weakSelf.page == 1) {
                [weakSelf.arrayFeedback removeAllObjects];
                if(manager.mutMgArray.count == 0){
                    weakSelf.tableViewOpinion.mj_footer.hidden = YES;
                }else{
                    [weakSelf.arrayFeedback setArray:manager.mutMgArray];
                }
            }
            else{
                [weakSelf.arrayFeedback addObjectsFromArray:manager.mutMgArray];
            }
            if(manager.mutMgArray.count == 0){
                [weakSelf.tableViewOpinion.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.tableViewOpinion reloadData];
        }else{
            [weakSelf setStringWarning:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的反馈";
    self.view.backgroundColor = [UIColor whiteColor];
    self.arrayFeedback = [[NSMutableArray alloc]init];
    self.tableViewOpinion = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableViewOpinion.delegate = self;
    self.tableViewOpinion.dataSource = self;
    self.tableViewOpinion.separatorColor = divider;
    self.tableViewOpinion.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.tableViewOpinion.estimatedRowHeight =0;
    self.tableViewOpinion.estimatedSectionFooterHeight =0;
    self.tableViewOpinion.estimatedSectionHeaderHeight =0;
    [self.view addSubview:self.tableViewOpinion];
    [self.tableViewOpinion registerClass:[LSZHistoryTableViewCell class] forCellReuseIdentifier:@"LSZHistoryTableViewCell"];
    [self.tableViewOpinion registerClass:[LSZResponseServiceTableViewCell class] forCellReuseIdentifier:@"LSZResponseServiceTableViewCell"];
    self.page = 1;
    [self getData:self.page];
    if (ScreenWidth == 320) {
        //5
        self.tableViewOpinion.separatorInset = UIEdgeInsetsMake(0, 40, 0, 20);
    }else if(ScreenWidth == 375){
        // 6 7
        self.tableViewOpinion.separatorInset = UIEdgeInsetsMake(0, 45, 0, 20);
    }else {
        //plus
        self.tableViewOpinion.separatorInset = UIEdgeInsetsMake(0, 50, 0, 20);
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.arrayFeedback.count;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.arrayFeedback.count > section) {
        LSZFeedbackListMg * mg = nil;
        mg = self.arrayFeedback[ section];
        if (mg.reply.length > 0) {
            return 2;
        }else{
            return 1;
        }
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     int a ;
    int b ;
    if (ScreenWidth == 320) {
        a = 13;
        b = 45;
    }else if(ScreenWidth == 375){
        a = 13.5;
        b = 50;
    }else {
        a = 14;
        b = 55;
    }
    LSZFeedbackListMg * mg = nil;
    mg = self.arrayFeedback[indexPath.section];
    NSString * gnoteString = [NSString stringWithFormat:@"%@",mg.gnote];
    if (indexPath.row == 0) {
        CGFloat widthHeight = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth- b height:ScreenHeight font:a :gnoteString];
        if (ScreenWidth == 320) {
            return  40+widthHeight-5;
        }else if(ScreenWidth == 375){
            return 45+widthHeight-5;
        }else {
            return 50+widthHeight-5;
        }
    }else{
        NSString * string = [NSString stringWithFormat:@"客服回复:%@",mg.reply];
        CGFloat widthHeight = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth- b height:ScreenHeight font:a :string];
        return widthHeight;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat a;
    if (ScreenWidth == 320) {
        a = 13;
    }else if(ScreenWidth == 375){
        a = 13.5;
    }else {
        a = 14;
    }
    if (indexPath.row == 0) {
        LSZHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZHistoryTableViewCell" forIndexPath:indexPath];
        if (self.arrayFeedback.count >indexPath.section) {
            LSZFeedbackListMg * mg = nil;
            mg = self.arrayFeedback[indexPath.section];
            [cell.imageViewHeader sd_setImageWithURL:[NSURL URLWithString:mg.avatar] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            if(mg.username.length > 0){
                cell.labelName.text = mg.username;
            }else{
                cell.labelName.text = [NSString stringWithFormat:@"fy_%@",mg.uid];
            }
            cell.labelTime.text = mg.add_time;
            cell.labelType.text = mg.status;
            NSString * gnoteString = [NSString stringWithFormat:@"%@",mg.gnote];
            cell.labelTitle.text = gnoteString;
            CGFloat widthHeight = [LSZSpecialPackaging caculateHeightOfText:cell.labelTitle.frame.size.width height:ScreenHeight font:a :cell.labelTitle.text];
            CGRect rect = cell.labelTitle.frame;
            rect.size.height = widthHeight;
            cell.labelTitle.frame = rect;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZResponseServiceTableViewCell * cell = [tableView  dequeueReusableCellWithIdentifier:@"LSZResponseServiceTableViewCell" forIndexPath:indexPath];
        if (self.arrayFeedback.count >indexPath.section) {
            LSZFeedbackListMg * mg = nil;
            mg = self.arrayFeedback[indexPath.section];
//
           NSString * string = [NSString stringWithFormat:@"客服回复:%@",mg.reply];
            NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:string];
            //获取要调整颜色的文字位置,调整颜色
            NSRange range1=[[hintString string]rangeOfString:@"客服回复:"];
            [hintString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range1];
             cell.labelTitle.attributedText = hintString;

            CGFloat widthHeight = [LSZSpecialPackaging caculateHeightOfText:cell.labelTitle.frame.size.width height:ScreenHeight font:a :cell.labelTitle.text];
            CGRect rect = cell.labelTitle.frame;
            rect.size.height = widthHeight;
            cell.labelTitle.frame = rect;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.arrayFeedback.count >indexPath.section) {
//        LSZFeedbackListMg * mg = nil;
//        mg = self.arrayFeedback[indexPath.section];
//        LSZFeedbackDetailsVC * vc = [[LSZFeedbackDetailsVC alloc]init];
//        vc.mg = mg;
//        [self.navigationController pushViewController:vc animated:YES];
//    }
}

@end
